<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/4 0004
 * Time: 下午 3:42
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use PhpOffice\PhpSpreadsheet\IOFactory;
define('DS', DIRECTORY_SEPARATOR);
defined('APP_PATH') or define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . DS);
defined('ROOT_PATH') or define('ROOT_PATH', dirname(realpath(APP_PATH)) . DS);
/**
 * 单位管理
 * Class Department
 * @package app\admin\controller
 */
class Department extends Authinfo
{
    //部门分类列表
    public function index_department(){
        $map=[];
        $name=input('name');
        if($name){
            $map[]=array('name','like','%'.$name.'%');
        }
        // 分页
        $current=input('current');
        if(!$current){
            $current=1;
        }
        $pagenums=input('pagenums');
        if(!$pagenums){
            $pagenums=50;
        }
        $nowpagenums=($current-1)*$pagenums;
        $order = input('order');
        if($order){
            $order='id DESC';
        }// 排序
        if($map){
            $total=Db::name('Department')->where($map)->where('pid','neq',0)->count('id');
            $info=Db::name('Department')
                ->where($map)
                ->where('pid','neq',0)
                ->order($order)
                ->limit($nowpagenums,$pagenums)
                ->select();
        }else{
            $total=Db::name('Department')->where('pid','neq',0)->count('id');
            $info=Db::name('Department')
                ->where('pid','neq',0)
                ->order($order)
                ->limit($nowpagenums,$pagenums)
                ->select();
        }
        foreach($info as $k=>$v){
            $info[$k]['pid']=Db::name('Department')->where(['id'=>$v['pid']])->value('name');
            $count=Db::name('cat')->where(['department_id'=>$v['name']])->select();
            if($count){
                $info[$k]['is_have']='拥有项目';
                $data['is_have']='1';
                Db::name('Department')->where(['id'=>$v['id']])->update($data);
            }else{
                $info[$k]['is_have']='无项目';
                $data['is_have']='0';
                Db::name('Department')->where(['id'=>$v['id']])->update($data);
            }

        }
        if($info){
            $datas['status']=1;
            $datas['info']=$info;
            $datas['total']=$total;
            $datas['current']=$current;
        }else{
            $datas['status']=0;
            $datas['info']='无数据！';
            $datas['total']=$total;
            $datas['current']=$current;
        }
        return json($datas);
    }

    //新增表中单位类别
    public function list_department(){
        $list=Db::name('Department')->field('id,pid,name')->select();
//        foreach($list as $k => $v){
//            $list[$k]['son']=Db::name('Department')->where('pid',$v['id'])->field('id,pid,name')->select();
//        }

        if($list){
            $datas['status']=1;
            $datas['info']=$list;
        }else{
            $datas['status']=0;
            $datas['info']='无数据！';
        }
        return json($datas);
    }
    //新增部门分类
    public function add_department(){
        $name=input('name');
        $info=[];
        $datas=[];
        if($name){
            $info['name']=$name;
        }
        $pid=input('pid');
        if($pid){
            $info['pid']=Db::name('Department')->where(['name'=>$pid])->value('id');
        }
        $is_have=input('is_have');
        if($is_have){
            $info['is_have']=$is_have;
        }
        $info['create_time']=time();
//        dump($info);exit;
        if($info){
            $id = Db::name('Department')->insertGetId($info);
            if($id){
                $datas['status']=1;
                $datas['info']='新增成功！';
            }else{
                $datas['status']=0;
                $datas['info']='无数据！';
            }
        }
        return json($datas);
    }

    //部门编辑回显
    public function edit_view(){
        $id=input('id');
        $list=Db::name('Department')->where(['id'=>$id])->find();
        $list['pid']=Db::name('Department')->where(['id'=>$list['pid']])->value('name');
       if($list){
            $datas['status']=1;
            $datas['info']=$list;
        }else{
            $datas['status']=0;
            $datas['info']='无数据!';
        }
        return json($datas);
    }
    //部门分类编辑
    public function edit_department(){
        $id=input('id');
        $info=[];
        $datas=[];
        if($id){
            $name=input('name');
            if($name){
                $info['name']=$name;
            }
            $pid=input('pid');
            if($pid){
                $info['pid']=Db::name('Department')->where(['name'=>$pid])->value('id');
            }
            $is_have=input('is_have');
            if($is_have){
                $info['is_have']=$is_have;
            }
//            dump($info);exit;
            if($info){
                $result = Db::name('Department')->where(['id'=>$id])->update($info);
                if($result){
                    $datas['status']=1;
                    $datas['info']='修改成功！';
                }else{
                    $datas['status']=0;
                    $datas['info']='无数据！';
                }
            }else{
                $datas['status']=0;
                $datas['info']='无数据！';
            }
        }else{
            $datas['status']=0;
            $datas['info']='无参数！';
        }
        return json($datas);
    }

    //部门分类删除
    public function delete_department(){
        $ids=input('id');
        if(is_array($ids)){
            $info=Db::name('Department')->where('id','in',$ids)->delete();
            if($info){
                $datas['status']=1;
                $datas['info']=$info;
            }else{
                $datas['status']=0;
                $datas['info']='无数据！';
            }
        }else{
            $datas['status']=0;
            $datas['info']='无参数！';
        }
        return json($datas);
    }

    public function pladd(){
        $pathurl=input('pathurl');
//        $pathurl='uploads\wenjian\20180619\20180804.xlsx';
//        截取获得文档标题
//        $data=substr($pathurl,-13);
        $startRow=input('startRow');

        if(!$startRow){
            $startRow=2;
        }
        if($pathurl){
            $lujing=$pathurl;
            $excelFile=ROOT_PATH.$lujing;
//            $excelReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
            $excelReader =IOFactory::createReader('Xlsx');
            $excelReader->setReadDataOnly(true);
            $endRow = input('endRow');
            if (!$endRow) {
                $endRow = 2000;
            }

            $filterSubset=new Ireadefilter($startRow, $endRow,range('A', 'B'));
            $excelReader->setReadFilter($filterSubset);
            $phpexcel    = $excelReader->load($excelFile);
//			$activeSheet = $phpexcel->setActiveSheetIndex(9);

            $activeSheet = $phpexcel->getActiveSheet();

            $zongshu=$activeSheet->getHighestRow(); //总行数
            if($endRow>$zongshu){
                $endRow=$zongshu;
            }
            if($startRow<=$zongshu){

                $sheetData = $activeSheet->toArray(null, true, true, true);

                $highestColumn      = $activeSheet->getHighestColumn(); //最后列数所对应的字母，例如第2行就是B

                $data = array();

                for ($col = $startRow; $col <=$endRow; $col++) {
                    if($sheetData[$col]){
                        $data[] = $sheetData[$col];
                    }


                }
                $phpexcel->disconnectWorksheets();
                unset($phpexcel);

                $shujudata=array();


                if($data){
                    $nums=0;
                    foreach ($data as $key => $value){
                        $shujudata=array();
                        switch(trim($value['A'])) {
                            case "县级部门":
                                $dep_pid = 1;
                                break;
                            case "乡镇（街道）":
                                $dep_pid = 2;
                                break;
                            case "企业":
                                $dep_pid = 3;
                                break;
                        }

                        $shujudata['pid']=$dep_pid;
                        $shujudata['name']=$value['B'];

                        if($value['B']){//干部sfid存在
                            //查询是否存在数据库中
                            $cz=Db::name('Department')
                                ->where('name',$value['B'])
                                ->find();
                            if(!$cz){
                                //不存在则进行添加操作
                                $shujudata['create_time']=time();
                                $idss=Db::name('Department')->insert($shujudata);
                                if($idss){
                                    $nums++;
                                    unset($shujudata);
                                }
                            }else{//存在则进行重复数据统一操作
                                $shujudata['create_time']=time();
                                $gx=Db::name('Department')->where('id',$cz['id'])->update($shujudata);
                                $nums++;
                                unset($shujudata);
                            }
                        }
                    }
                    $datas['status']=1;
                    $tiaoshu=$endRow;
                    $datas['cginfo']=$nums;
                    $datas['nextnums']=$endRow+1;
                    $datas['isend']=0;
                    unset($data);

                }else{
                    $datas['status']=0;
                    $datas['info']='无数据！';

                }


            }else{
                $datas['status']=1;
                $datas['info']='导入完毕';
                $datas['isend']=1;

            }


        }else{
            $datas['status']=0;
            $datas['info']='文件不存在';


        }
        return  json($datas);

    }
    //将科学计数法转换为原始数字字符串的方法
    function NumToStr($num){
        if (stripos($num,'e')===false) return $num;
        $num = trim(preg_replace('/[=\'"]/','',$num,1),'"');//出现科学计数法，还原成字符串
        $result = "";
        while ($num > 0){
            $v = $num - floor($num / 10)*10;
            $num = floor($num / 10);
            $result = $v . $result;
        }
        return $result;
    }
}